package com.situ.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;




import com.alipay.api.FileItem;
import com.situ.bean.Order;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {

    public String code(Order order) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        // 设置商户订单号

        model.setOutTradeNo(order.getCode());
        // 设置订单总金额
        model.setTotalAmount(order.getMoney().toString());
        // 设置订单标题
        model.setSubject(String.valueOf(order.getUserid()));
        // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721061145199");
        // 设置商户的原始订单号


        model.setMerchantOrderNo("20161008001");

        request.setBizModel(model);


        request.setNotifyUrl("http://6bf7512.r11.cpolar.top/api/orders/paysuccess");
        // 设置订单附加信息
//        model.setBody("Iphone6 16G");

//        // 设置订单包含的商品列表信息
//        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
//        GoodsDetail goodsDetail0 = new GoodsDetail();
//        goodsDetail0.setGoodsName("ipad");
//        goodsDetail0.setQuantity(1L);
//        goodsDetail0.setPrice("2000");
//        goodsDetail0.setGoodsId("apple-01");
//        goodsDetail0.setGoodsCategory("34543238");
//        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
//        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
//        goodsDetail.add(goodsDetail0);
//        model.setGoodsDetail(goodsDetail);
//
//        // 设置业务扩展参数
//        ExtendParams extendParams = new ExtendParams();
//        extendParams.setSysServiceProviderId("2088511833207846");
//        model.setExtendParams(extendParams);
//
//        // 设置商户传入业务信息
//        BusinessParams businessParams = new BusinessParams();
//        businessParams.setMcCreateTradeIp("127.0.0.1");
//        model.setBusinessParams(businessParams);
//
//        // 设置可打折金额
//        model.setDiscountableAmount("80.00");
//
//        // 设置商户门店编号
//        model.setStoreId("NJ_001");
//
//        // 设置商户操作员编号
//        model.setOperatorId("yx_001");
//
//        // 设置商户机具终端编号
//        model.setTerminalId("NJ_T_001");


        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }

       return response.getQrCode();
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC1fvpq28ghi7YW7PCeeR++pHgTkbtMc18T0toybW7T68aOLuKVH+3XjBe2ydNA1647kXptnHZ+yjvQWi8THBvwg3xPczEAbvMgW2IKuOd+eAoNzqpqZSAZKUed7X1ky84ru0HCEulB6S+z0zDZJNyKRTgB97KZYxw4d+KlMs2kZGh/77bV6TLASm+HKYkFSfmWTLj9vEP7H6HxFS7nHjiCvRIcrYVkFCfTE8nfX7saFo2PuYfKvdnL68eysFikWIiUy3fRP/bX1qTzouFyz8AI5u0Ryos/R4ZpYQe7hw36jzRmmsgFoh7psrEnn925P2B0bKz51DJFSA5x++vCwG2fAgMBAAECggEBAIZUtw+EH/oEJ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9XkiM6F4PvhMiu1Wht55yTb/B1yUisxrscXLJ8DfjugzG1fNhVz1hL5ujRSV3L55bt6fDVrr4NoyBIUPO74u3HLBiv9nVzaGVGkVVT7qijqYfw2eLSgwa0eOtOszYM8v8AyfR4jSW03U2";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnirKu7VVowJidKkn7IoNRfD/e8dLtMunOxnqJlSuj2S/yOmceYI6YXwBNdAiQmJVzHb4ye3hjnggCv3S9UDxM8SEZdQjetRYm0S+Pu/bNKJ4LYRkAMJb4CYqjHk33xStAHgzTyf2B/W4ZPYtULW9wnwa+4yHeJoC6vtuWfQsXaZhom279jYXh8cp3pebrBR6TB0IT/5/+rla4QRpUX8/OfWQFM07NeX12Jsa9f59ZghFdVE91JfIZgRqVazweyINbkyqq0p3ky9qciRvs77G1O/2DYvc9+vJ/uGTUjQ3nwQUEbX4XwlRxPHCEFIs3i9FnQm7Ed5UvV4IAcoRKb361wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000146604148");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }




}


